package com.laikan.reader.bookreader.activity;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.laikan.library.components.activitys.BasicActivity;
import com.laikan.library.httpvolley.engine.HttpApiRequestEngine;
import com.laikan.library.httpvolley.engine.Requester;
import com.laikan.library.httpvolley.helper.RequestListener;
import com.laikan.library.httpvolley.helper.VolleyErrorHandler;
import com.laikan.library.httpvolley.response.HttpResponse;
import com.laikan.library.managers.ToastMgr;
import com.laikan.library.utils.SPUtil;
import com.laikan.reader.R;
import com.laikan.reader.comment.APIProtocol;
import com.laikan.reader.comment.Constants;
import com.laikan.reader.core.adapter.FansAdapter;
import com.laikan.reader.core.bean.BaseDataBean;
import com.laikan.reader.core.bean.FansBean;
import com.laikan.reader.core.bean.FansTwo;
import com.laikan.reader.core.utils.LoadingDialog;
import com.laikan.reader.core.utils.SystemUtil;
import com.socks.library.KLog;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 投票页
 *
 * @author wxl
 */
public class Fans extends BasicActivity implements OnClickListener {

    private ImageView back;
    private String bookid, type;
    private BaseDataBean<FansBean<FansTwo>> result;
    private ListView fan_listview;
    private ArrayList<FansTwo> arraylist;
    private FansAdapter adapter;
    private TextView ranking;
    private RelativeLayout ranking_rela;
    private LoadingDialog mloadingDialog;

    @Override
    protected void assignViews() {
        setContentView(R.layout.lk_fans);
        arraylist = new ArrayList<FansTwo>();
        adapter = new FansAdapter(mContext, arraylist);
        back = (ImageView)findViewById(R.id.back);
        back.setOnClickListener(this);
        fan_listview = (ListView)findViewById(R.id.fan_listview);
        ranking = (TextView)findViewById(R.id.ranking);
        fan_listview.setAdapter(adapter);
        ranking_rela = (RelativeLayout)findViewById(R.id.ranking_rela);
    }

    @Override
    protected void initData() {
        mloadingDialog = new LoadingDialog(mContext);
        bookid = getIntent().getStringExtra("bookId");
        send();
    }

    @Override
    protected void setListener() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.back:
                finish();
                break;
            default:
                break;
        }
    }

    private void send() {
        mloadingDialog.show();
        Map<String, String> map = new HashMap<>();
        map.put("type", type);
        map.put("bookId", bookid);
        map.put("userId", SPUtil.getString("user_id", ""));
        RequestListener requestListener = new RequestListener() {
            @Override
            public void onPreExecute() { KLog.e("---------volley onPreExecute!!!"); }

            @Override
            public void onSuccess(HttpResponse httpResponse) {
                mloadingDialog.dismiss();
                try {
                    Gson gson = new Gson();
                    Type type = new TypeToken<BaseDataBean<FansBean<FansTwo>>>() {
                    }.getType();
                    result = gson.fromJson(httpResponse.responseContent, type);

                }catch (Exception e) {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                    e.printStackTrace();
                }

                if (result != null) {

                    if (null == result.getData() || !Constants.RESULT_OK.equals(result.getResult())) {

                        ToastMgr.showShortToast(result.getError_msg());
                    }else {
                        arraylist = result.getData().getFollowList();
                        if (arraylist.size() == 0) {
                            ranking_rela.setVisibility(View.GONE);
                        }else {
                            if (SystemUtil.isLogin()) {
                                ranking_rela.setVisibility(View.VISIBLE);
                                if (result.getData().getFollowRanking().equals("0")) {
                                    ranking.setText("你尚未进入本书的粉丝榜排名");
                                }else {
                                    String bookname = result.getData().getBookName();
                                    if (bookname.length() > 6) {
                                        bookname = bookname.substring(0, 6) + "...";
                                    }
                                    ranking.setText("你在《" + bookname + "》的粉丝榜中排名:" + result.getData().getFollowRanking());
                                }

                            }else {
                                ranking_rela.setVisibility(View.GONE);
                            }
                        }
                        adapter.setDatas(arraylist);
                    }
                }else {
                    ToastMgr.showShortToast(R.string.laikan_data_load_failed);
                }

            }

            @Override
            public void onError(VolleyErrorHandler errorHandler) {
                mloadingDialog.dismiss();
                ToastMgr.showShortToast(R.string.laikan_network_anomaly);
                KLog.e("网络不给力，请重试。15");
            }

            @Override
            public void onFinish() {

            }
        };
        Requester requester = new Requester();
        requester.setUrl(APIProtocol.laikan_fans + bookid + "&pageSize=99&userId=" + SPUtil.getString("user_id", ""));
//        requester.setMapBody(map);
        HttpApiRequestEngine.getInstance().httpGetRequest(requester, requestListener);

//        HttpApiRequestEngine.getInstance().httpGetRequest(APIProtocol.laikan_fans + bookid + "&pageSize=99&userId=" + SPUtil.getString("user_id", ""), null, null, requestListener);
    }

}
